package com.tanhua.manager.controller;

import com.tanhua.domain.mongo.UserFrozen;
import com.tanhua.manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/manage")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户翻页列表
     * GET/manage/users
     */
    @GetMapping("/users")
    public ResponseEntity findUserList(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       @RequestParam(required = false) Long id,
                                       @RequestParam(required = false) String nickname,
                                       @RequestParam(required = false) String city) {
        return userService.findUserList(page, pagesize, id, nickname, city);
    }

    /**
     * 用户基本资料
     * GET/manage/users/:userID
     */
    @GetMapping("/users/{id}")
    public ResponseEntity findUserById(@PathVariable("id") Long userId) {
        return userService.findUserById(userId);
    }

    /**
     * 动态分页
     * GET/manage/messages
     */
    @GetMapping("/messages")
    public ResponseEntity findPublishList(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer pagesize,
                                          @RequestParam(required = false) Long uid,
                                          @RequestParam(required = false)String state) {
        Integer status = null;
        if (state != null && !"".equals(state) && !"''".equals(state)) {
            status = Integer.valueOf(state);
        }
        return userService.findPublishList(page, pagesize, uid, status);

    }

    /**
     * 动态详情
     * GET/manage/messages/:id
     */
    @GetMapping("/messages/{id}")
    public ResponseEntity findPublishById(@PathVariable("id") String publishId) {
        return userService.findPublishById(publishId);
    }

    /**
     * 评论列表翻页
     * GET/manage/messages/comments
     * @param messageID 动态或视频id
     */
    @GetMapping("/messages/comments")
    public ResponseEntity findCommentList(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer pagesize,
                                          @RequestParam(required = false) String sortProp,
                                          @RequestParam(required = false) String sortOrder,
                                          @RequestParam(required = true) String messageID) {
        return userService.findCommentList(page, pagesize, sortProp, sortOrder, messageID);
    }

    /**
     * 用户视频列表
     * GET/manage/videos
     */
    @GetMapping("/videos")
    public ResponseEntity findVideoList(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize,
                                        Long uid) {
        return userService.findVideoList(page, pagesize, uid);
    }

    /**
     * 动态通过
     * POST manage/messages/pass
     */
    @PostMapping("/messages/pass")
    public ResponseEntity dynamicPass(@RequestBody String[] Ids){
        return userService.dynamicPass(Ids);
    }

    /**
     * 动态拒绝
     * POST /mamge/message/reject
     */
    @PostMapping("/messages/reject")
    public ResponseEntity dynamicRefusal(@RequestBody String[] Ids){

        return userService.dynamicRefusal(Ids);

    }

    /**
     * 用户冻结操作
     * POST/manage/users/freeze
     */
    @PostMapping("/users/freeze")
    public ResponseEntity freezeUser(@RequestBody Map map) {

        Long userId = Long.parseLong(map.get("userId").toString());
        Integer freezingTime = Integer.parseInt(map.get("freezingTime").toString());
        Integer freezingRange = Integer.parseInt(map.get("freezingRange").toString());
        String reasonsForFreezing = map.get("reasonsForFreezing").toString();
        String frozenRemarks = map.get("frozenRemarks").toString();
        UserFrozen userFrozen = new UserFrozen();
        userFrozen.setUserId(userId);
        userFrozen.setFreezingTime(freezingTime);
        userFrozen.setFreezingRange(freezingRange);
        userFrozen.setReasonsForFreezing(reasonsForFreezing);
        userFrozen.setFrozenRemarks(frozenRemarks);
        userFrozen.setOutTime();
        return userService.freezeUser(userFrozen);
    }

    /**
     * 用户解冻操作
     * POST/manage/users/unfreeze
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity thawUser(@RequestBody Map map) {
        int userIdInt = Integer.parseInt(map.get("userId").toString());
        Long userId = Long.valueOf(userIdInt);
        String reasonsForThawing = map.get("reasonsForThawing").toString();
        return userService.thawUser(userId);
    }
}
